/*jslint browser: true*/
/*global $, jQuery, alert, window*/
/**
等待界面
*/
window.id_of_settimeout = 0;
function hideLoading() {
    "use strict";
    window.id_of_settimeout = setTimeout(function () {
        $("#loading").hide();
    }, 500);
}
function loading() {
    "use strict";
    $("#loading").show();
    clearTimeout(window.id_of_settimeout);
}

/*去除字符串首尾的空格，中间的空格保留*/
function trim(s) {
    "use strict";
    return s.replace(/(^\s*)|(\s*$)/g, "");
}

/*检查是否存在与数组*/
function is_in_array(str_num, checked_array) {
    "use strict";
    var index = 0,
        length = checked_array.length,
        item;
    for (index = 0; index < length; index = index + 1) {
        item = checked_array[index];
        if (item === str_num) {
            return true;
        }
    }
    return false;
}

function get_random_list(random_time, random_list_length) {
    "use strict";
    var return_list = [],
        index,
        length = random_time,
        random_num,
        random_not_exist;
    for (index = 0; index < length; index = index + 1) {
        random_not_exist = true;
        while (random_not_exist) {
            random_num = parseInt(Math.random() * random_list_length, 10);
            if (!is_in_array(random_num, return_list)) {
                return_list.push(random_num);
                random_not_exist = false;
            }
        }
    }
    return return_list.sort();
}

/*将“2014/01/01”转换成数字20140101*/
function date_to_int(date_string) {
    "use strict";
    return parseInt(date_string.replace(/\//g, ""), 10);
}

/*将数字20140101转换成“2014/01/01”*/
function int_to_date(date_int) {
    "use strict";
    var date_reg = new RegExp('^([0-9]{4})([0-9]{2})([0-9]{2})$'),
        date_list = date_reg.exec(date_int);
    if (date_list) {
        date_list.shift();
        return date_list.join("/");
    }
    throw new Error("date_int is invalid :" + date_int);
}

/*根据user_id，查找user_name*/
function getUserName(user_id) {
    "use strict";
    var author_list = window.fetchData(window.storage("/user/"), window.format.author),
        index,
        length = author_list.length,
        author_item;
    window.getUserName = function (user_id) {
        for (index = 0; index < length; index = index + 1) {
            author_item = author_list[index];
            if (author_item.id === user_id) {
                return author_item.name;
            }
        }
        return "";
    };
    return window.getUserName(user_id);
}

/*update 中，查找变化的值*/
function getChangedValue(jsData, serverData, transFormat) {
    "use strict";
    var transData = {},
        server_var_name,
        js_var_name,
        server_var,
        js_var,
        is_empty = true;
    for (server_var_name in transFormat) {
        if (transFormat.hasOwnProperty(server_var_name)) {
            js_var_name = transFormat[server_var_name];
            js_var = jsData[js_var_name];
            server_var = serverData[server_var_name];
            if (server_var instanceof Array) {
                if (server_var.toString() !== js_var.toString()) {
                    transData[js_var_name] = js_var;
                    is_empty = false;
                }
            } else {
                if (server_var !== js_var) {
                    transData[js_var_name] = js_var;
                    is_empty = false;
                }
            }
        }
    }
    if (is_empty) {
        return false;
    } else {
        return transData;
    }
}

/*在试卷中，根据id和选项数生成raido*/
function get_answer_radio(q_id, q_len) {
    "use strict";
    var answer_list = ["A", "B", "C", "D", "E", "F", "G", "H"],
        input_radio = '<input type="radio" class="answer_{id}"' +
            ' name="answer_{id}" value="{name}">{name}</input>',
        return_list = [],
        index,
        posi = 0,
        time;
    for (index = 0; index < 4; index += 1) {
        return_list.push(input_radio.format({
            name: answer_list[index],
            id: q_id
        }));
        return_list.push('<br/>');
    }
    for (time = 1; index < q_len; index += 1) {
        posi = (index - 4) * 2 + time;
        time += 1;
        return_list.splice(posi,
            0,
            input_radio.format({
                name: answer_list[index],
                id: q_id
            }));
    }
    return return_list.join("");
}

window.view = {};
window.sync = {};
window.init = {};
window.init_func_list = [];
window.listener = {};

function start_funciton() {
    "use strict";
    window.localStorage.setItem("pythonems", "true");
    window.sessionStorage.setItem("pythonems", "true");
    //init html detail
    var init_func_list = window.init_func_list,
        index,
        length = init_func_list.length,
        init_func_name,
        init_func;
    for (index = 0; index < length; index = index + 1) {
        init_func_name = init_func_list[index];
        //window.console.log("init:" + init_func_name);
        window.init[init_func_name]();
    }
}

$(document).ready(function () {
    "use strict";
    if (window.sessionStorage.getItem("pythonems")) {
        start_funciton();
    } else {
        setTimeout(function () {
            //clear localstorage at first time
            if (window.localStorage.getItem("pythonems") !== "true") {
                window.localStorage.clear();
            }
            //init
            start_funciton();
        }, 1000);
    }
});